package 单周赛.history;

import sun.security.util.BitArray;

import java.util.*;

/**
 *
 */
public class 第293场单周赛 {

    public static void main(String[] args) {

        String[] words = {"a", "b", "c", "d", "e"};
        System.out.println(new 第293场单周赛().removeAnagrams(words));

        int[] special = {35, 48};
        System.out.println(maxConsecutive(28, 50, special));

        int[] candidates = {16, 17, 71, 62, 12, 24, 14};
        System.out.println(largestCombination(candidates));

        BitArray bitArray = new BitArray(5);
        System.out.println(bitArray.get(1));

    }

    /**
     * 使用栈来模拟
     *
     * @param words
     * @return
     */
    public List<String> removeAnagrams(String[] words) {
        Deque<String> stack = new ArrayDeque<>();
        List<String> list = new ArrayList<>();
        stack.addFirst(words[0]);
        int index = 1;
        while (index < words.length) {
            while (index < words.length && isAnagram(words[index], stack.peekFirst())) {
                index++;
            }
            if (index < words.length) {
                stack.addFirst(words[index]);
            }
        }
        while (!stack.isEmpty()) {
            list.add(stack.pollLast());
        }
        return list;
    }

    public boolean isAnagram(String str1, String str2) {
        if (str1.length() != str2.length()) {
            return false;
        }
        int[] arr1 = new int[26];
        int[] arr2 = new int[26];
        for (int i = 0; i < str1.length(); i++) {
            arr1[str1.charAt(i) - 'a']++;
            arr2[str2.charAt(i) - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 排序后模拟
     *
     * @param bottom
     * @param top
     * @param special
     * @return
     */
    public static int maxConsecutive(int bottom, int top, int[] special) {
        int ans = 0;
        Arrays.sort(special);
        ans = Math.max(ans, special[0] - bottom);
        ans = Math.max(ans, top - special[special.length - 1]);

        for (int i = 1; i < special.length; i++) {
            ans = Math.max(ans, special[i] - special[i - 1] - 1);
        }
        return ans;
    }

    /**
     * 使用一个数组存储bit位 == 1的数量
     * 因为只需要答案大于0，那么我们贪心的统计最大数量的某一个bit的即可
     *
     * @param candidates
     * @return
     */
    public static int largestCombination(int[] candidates) {
        int ans = 0;
        int[] bits = new int[32];
        for (int candidate : candidates) {
            for (int i = 0; i < 32; i++) {
                if (((candidate >> i) & 1) == 1) bits[i]++;
            }
        }
        for (int bit : bits) {
            ans = Math.max(ans, bit);
        }
        return ans;
    }

}
